/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import systole.domain.report.template.Conclusion;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.ConclusionBroker;
import systole.view.messages.ErrorMessages;

/**
 *
 * @author jmj
 */
public class ConclusionBrokerDB extends BrokerDB implements ConclusionBroker {

    /**
     * 
     */
    public ConclusionBrokerDB() {
        super();
    }

    public void insert(Conclusion conclusion) throws ExceptionDAO {
        try {
            this.logger.logDebug("save conclusion ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(conclusion);
            this.logger.logDebug("save successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on save conclusion, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public void update(Conclusion conclusion) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating conclusion ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(conclusion);
            this.logger.logDebug("update successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on update conclusion, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public List<Conclusion> getAllConclusions() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting all Conclusions");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<Conclusion> list = currentSession.createQuery("from Conclusion c order by upper(c.name)").list();
            this.logger.logDebug("getting Conclusions successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all Conclusions, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las Conclusones", e.fillInStackTrace());
        }
    }

    public List<Conclusion> getConclusionsForMale() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting all male Conclusions");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<Conclusion> list = currentSession.createCriteria(Conclusion.class).add(Restrictions.eq("male", true)).list();
            this.logger.logDebug("getting Conclusions successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all male Conclusions, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las Conclusiones", e.fillInStackTrace());
        }
    }

    public List<Conclusion> getConclusionsForFemale() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting female Conclusions");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<Conclusion> list = currentSession.createCriteria(Conclusion.class).add(Restrictions.eq("female", true)).list();
            this.logger.logDebug("getting Conclusions successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all female Conclusions, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las Conclusiones", e.fillInStackTrace());
        }
    }

    @SuppressWarnings("unchecked")
    public boolean existConclusion(String name, Integer id) throws ExceptionDAO {
        try {
            this.logger.logDebug("exist Conclusion");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(Conclusion.class).
                    add((id == null) ? Restrictions.eq("name", name)
                    : Restrictions.and(Restrictions.eq("name", name), Restrictions.ne("id", id)));
            List list = criteria.list();
            this.logger.logDebug("exist Conclusion successfully");
            return !list.isEmpty();
        } catch (HibernateException e) {
            this.logger.logError("error on exist Conclusion, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo verificar si ya existe la conclusión", e.fillInStackTrace());
        }
    }
}
